#!/usr/bin/env bash
cd /var/local
if [ ! -d "./web/" ];then
  git clone https://github.com/demo2099/web.git
  else
  echo "文件夹已经存在"
fi
#rm -rf web
#git clone http://124.223.11.108:13000/demo2099/web.git
rm -rf  nginx
mkdir nginx
mkdir -p /var/local/nginx/{conf,html,logs}
touch /var/local/nginx/html/index.html
echo   "<html><body><h1> xxxxxxxxxxxxxe</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Wed Sep 09 15:07:28 CST 2020</div><div>There was an unexpected error (type=Not Found, status=404).</div><div>No message available</div></body></html>" >> /var/local/nginx/html/index.html
touch /var/local/nginx/conf/nginx.conf
cat >/var/local/nginx/conf/nginx.conf  <<EOF
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

stream {
	map \$ssl_preread_server_name \$backend_name {
		vikytwotoo.ga trojan;
		$1 tls_backend;
		default web;
	}

	upstream web {
		server 127.0.0.1:80;
	}

	upstream trojan {
		server 127.0.0.1:23342;
	}

	upstream tls_backend {
		server 127.0.0.1:8443;
	}

	server {
		listen 443 reuseport;
		listen [::]:443 reuseport;
		proxy_pass  \$backend_name;
		ssl_preread on;
	}
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  ' -  [] "" '
                      '  "" '
                      '"" ""';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://\$host\$request_uri;
}

server {
        listen 8443 ssl http2;
        server_name $1;
        ssl_certificate /etc/v2ray-agent/tls/$1.crt;
        ssl_certificate_key /etc/v2ray-agent/tls/$1.key;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_stapling on;
        ssl_stapling_verify on;
        port_in_redirect off; # 重要：阻止nginx重定向到此Server listen的端口

}

   
}
EOF
x=$[RANDOM%10+1]
echo "随机网站love${x}"
rm -rf /var/local/web/love1/*
wget -c -P /usr/share/nginx "https://raw.githubusercontent.com/mack-a/v2ray-agent/master/fodder/blog/unable/html${x}.zip" >/dev/null
unzip -o "/usr/share/nginx/html${x}.zip" -d /usr/share/nginx/html >/dev/null
rm -f "/usr/share/nginx/html${x}.zip*"
cp -r /usr/share/nginx/html/* /var/local/web/love1/
docker rm -f mynginx
docker run  -d --restart=always --name mynginx  --network host -v /etc/v2ray-agent/tls/:/etc/v2ray-agent/tls/ -v /var/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /var/local/nginx/logs:/var/log/nginx  -v /var/local/web/love1:/usr/share/nginx/html nginx